//
//  AutoReleasePoolViewController.m
//  ExerciseOC
//
//  Created by 王子曦 on 2025/4/14.
//

#import "AutoReleasePoolViewController.h"

@interface AutoReleasePoolViewController ()
@property (nonatomic, strong) UITextView *textView;
@end

@implementation AutoReleasePoolViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"自动释放池";
    
    // 创建文本视图
    self.textView = [[UITextView alloc] initWithFrame:self.view.bounds];
    self.textView.editable = NO;
    self.textView.font = [UIFont systemFontOfSize:16];
    [self.view addSubview:self.textView];
    
    // 设置内容
    NSMutableString *content = [NSMutableString string];
    [content appendString:@"自动释放池（Autorelease Pool）是iOS中的延迟释放机制\n\n"];
    
    [content appendString:@"1. 自动释放池的作用：\n"];
    [content appendString:@"   - 延迟释放对象\n"];
    [content appendString:@"   - 统一管理内存释放时机\n"];
    [content appendString:@"   - 避免频繁释放内存\n\n"];
    
    [content appendString:@"2. 自动释放池的使用场景：\n"];
    [content appendString:@"   - 临时生成大量对象\n"];
    [content appendString:@"   - 循环中创建临时对象\n"];
    [content appendString:@"   - 自定义NSOperation\n"];
    [content appendString:@"   - 非主线程执行任务\n\n"];
    
    [content appendString:@"3. 代码示例：\n"];
    [content appendString:@"   @autoreleasepool {\n"];
    [content appendString:@"       // 创建临时对象\n"];
    [content appendString:@"       NSString *str = [[NSString alloc] init];\n"];
    [content appendString:@"       str = [str autorelease];\n"];
    [content appendString:@"   }  // 池释放，对象被释放\n\n"];
    
    [content appendString:@"4. ARC下的自动释放池：\n"];
    [content appendString:@"   - 使用@autoreleasepool块\n"];
    [content appendString:@"   - 编译器自动管理autorelease\n"];
    [content appendString:@"   - 主线程RunLoop自动创建和释放\n\n"];
    
    [content appendString:@"5. 注意事项：\n"];
    [content appendString:@"   - 避免在循环中使用autorelease\n"];
    [content appendString:@"   - 自动释放池可以嵌套使用\n"];
    [content appendString:@"   - 及时释放大内存对象\n"];
    [content appendString:@"   - 在非主线程手动创建自动释放池"];
    
    self.textView.text = content;
}

@end